﻿using System;
using B.S.DataBase.MyProject.Domain.Departments;
using B.S.DataBase.MyProject.Domain.RBAC;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.Users;
using B.S.DataBase.MyRead.API.DTOs;
using Consul;
using MediatR;
using static Humanizer.In;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.Users
{
    public class GetUserCommandHandler : IRequestHandler<GetUserCommand, APIResult<List<UserDTOs>>>
    {
        private readonly IBaseRepository<User> userRepos;
        private readonly IBaseRepository<MyProject.Domain.RBAC.Role> roleRepos;

        private readonly IBaseRepository<DepartModel> departModelRepos;


        public GetUserCommandHandler(IBaseRepository<User> userRepos, IBaseRepository<MyProject.Domain.RBAC.Role> roleRepos, IBaseRepository<DepartModel> departModelRepos)
        {
            this.userRepos = userRepos;
            this.roleRepos = roleRepos;
            this.departModelRepos = departModelRepos;
        }

        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<List<UserDTOs>>> Handle(GetUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<UserDTOs>> result = new APIResult<List<UserDTOs>>();
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();


            #region 我的User显示
            //var list = userRepos.GetAll().Result.Where(x=>x.IsDel == false);
            //var lian = from a in userRepos.GetAll().Result
            //           join b in userRoleRepos.GetAll().Result on a.UserId equals b.UserId
            //           join c in roleRepos.GetAll().Result on b.RoleId equals c.RoleId
            //           where a.IsDel == false
            //           select new UserDTOs
            //           {
            //               UserId = a.UserId,
            //               DepId = a.UserId,
            //               UserName = a.UserName,
            //               Password = a.Password,
            //               NickName = a.NickName,
            //               Sex = a.Sex,
            //               ProvinceId = a.ProvinceId,
            //               CityId = a.CityId,
            //               CountyId = a.CountyId,
            //               HamletName = a.HamletName,
            //               CreateName = a.CreateName,
            //               CreateTime = a.CreateTime,
            //               UpdateName = a.UpdateName,
            //               UpdateTime = a.UpdateTime,
            //               IsDel = a.IsDel,
            //               RoleName = c.RoleName,
            //           };
            //result.data = lian.ToList();
            #endregion

            #region
            //var list = userRepos.GetAll().Result
            //.Join(userRoleRepos.GetAll().Result, a => a.UserId, b => b.UserId, (a, b) => new { a, b })
            //.Join(roleRepos.GetAll().Result, temp => temp.b.RoleId, o => o.RoleId, (temp, o) => new UserDTOs
            //{
            //    UserId = temp.a.UserId,
            //    UserName = temp.a.UserName,
            //    DepId = temp.a.DepId,
            //    NickName = temp.a.NickName,
            //    Sex = temp.a.Sex,
            //    ProvinceId = temp.a.ProvinceId,
            //    CityId = temp.a.CityId,
            //    CountyId = temp.a.CountyId,
            //    HamletName = temp.a.HamletName,
            //    RoleId = o.RoleId,
            //    RoleName = o.RoleName,
            //    UserRoleId = temp.b.UserRoleId,
            //    CreateName = temp.a.CreateName,
            //    CreateTime = temp.a.CreateTime,
            //    UpdateName = temp.a.UpdateName,
            //    UpdateTime = temp.a.UpdateTime,
            //    IsDel = temp.a.IsDel,
            //}).GroupBy(x => x.UserId) // 按用户编号分组
            //    .Select(group => new UserDTOs
            //    {
            //        UserId = group.Key,
            //        UserName = group.First().UserName,
            //        DepId = group.First().DepId,
            //        NickName = group.First().NickName,
            //        Sex = group.First().Sex,
            //        ProvinceId = group.First().ProvinceId,
            //        CityId = group.First().CityId,
            //        CountyId = group.First().CountyId,
            //        HamletName = group.First().HamletName,
            //        CreateName = group.First().CreateName,
            //        CreateTime = group.First().CreateTime,
            //        UpdateName = group.First().UpdateName,
            //        UpdateTime = group.First().UpdateTime,
            //        RoleId = group.First().RoleId,
            //        RoleName = string.Join(", ", group.Select(x => x.RoleName)), // 合并角色名称
            //        UserRoleId = group.First().UserRoleId,
            //        IsDel = group.First().IsDel
            //    }).Where(x => x.IsDel == false);
            #endregion

            var list = from a in await roleRepos.GetAll()
                       join b in await userRepos.GetAll()
                       on a.RoleId equals b.RoleId
                       join c in await departModelRepos.GetAll()
                       on b.DepId equals c.DepartId
                       select new UserDTOs
                       {

                           UserId = b.UserId,
                           DepId = b.DepId,
                           RoleId = b.RoleId,
                           UserName = b.UserName,
                           NickName = b.NickName,
                           Sex = b.Sex,
                           ProvinceId = b.ProvinceId,
                           CityId = b.CityId,
                           CountyId = b.CountyId,
                           HamletName = b.HamletName,
                           CreateName = b.CreateName,
                           CreateTime = b.CreateTime,
                           UpdateName = b.UpdateName,
                           UpdateTime = b.UpdateTime,
                           RoleName =a.RoleName,                     
                           DepartName =c.DepartName,
                       };


            result.data = list.OrderByDescending(x=>x.UserId).ToList();
            return await Task.FromResult(result);
        }
    }
}
